<?php

namespace app\models;

use yii\db\ActiveRecord;

use yii\data\ActiveDataProvider;

class Supplier extends ActiveRecord
{
    public static function tableName()
    {
        return '{{%supplier}}';
    }

    public function rules()
    {
        return [
            [
                [ 'id', 'name', 'code', 't_status'],
                'trim'
            ],
            [
                [ 'id' ],
                'integer'
            ],
            [
                ['name', 'code', 't_status' ],
                'string'
            ],
        ];
    }

    //查询
    public function search($params)
    {
        $query = self::find();
        $provider = new ActiveDataProvider([
            'query' => $query,
            //设置分页参数
            'pagination' => [
                //分页大小
                'pageSize' => 3,
                //设置地址栏当前页数参数名
                'pageParam' => 'p',
                //设置地址栏分页大小参数名
                'pageSizeParam' => 'pageSize',
            ],
            //设置排序
            'sort' => [
                //默认排序方式
                'defaultOrder' => [
                    'id' => SORT_DESC,
                ],
                //参与排序的字段
                'attributes' => [
                    'id', 'code'
                ],
            ],
        ]);

        if (!($this->load($params) && $this->validate())) {
            return $provider;
        }

        //增加过滤条件
        $query->andFilterWhere(['id' => $this->id])
            ->andFilterWhere(['like', 'name', $this->name])
            ->andFilterWhere(['like', 'code', $this->code])
            ->andFilterWhere(['t_status' => $this->t_status]);

        return $provider;

    }

}